using Sidvall.CodeGenerator.Xml;
using System.Xml.Linq;

namespace Sidvall.CodeGenerator
{
    public class ResourceFileHelper
    {
        #region Public Members

        #region NewDomainModel

        public static DomainModel NewDomainModel(string fileName, string domainObjectName, string domainObjectTag)
        {
            var settings = new ResourceFileImportSettings();
            settings.ResourceFiles.Add(fileName, domainObjectName, domainObjectTag);
            return NewDomainModel(settings);
        }
        public static DomainModel NewDomainModel(ResourceFileImportSettings settings)
        {
            if (settings == null)
                return null;
            if (settings.ResourceFiles.Count == 0)
                return null;

            var domainModel = new DomainModel();
            foreach (var resourceFiles in settings.ResourceFiles)
            {
                var item = new DomainObject()
                {
                    Name = resourceFiles.DomainObjectName,
                    Tag = resourceFiles.DomainObjectTag,
                };
                item = domainModel.DatabaseDomainObjects.AddOrGetExisting(item);
                var document = XDocument.Load(resourceFiles.FileName);
                var elements = document.Root.GetElements("data");
                foreach (var element in elements)
                {
                    var domainObjectProperty = new DomainObjectProperty()
                    {
                        Comment = element.GetElementValue("comment"),
                        DefaultValue = element.GetElementValue("value"),
                        IsComputed = false,
                        IsComputedInDataStore = false,
                        IsIdentity = false,
                        IsIdentityInDataStore = false,
                        IsKey = false,
                        IsNullable = false,
                        IsNullableInDataStore = false,
                        MaxLength = null,
                        MaxDecimalCount = null,
                        Name = element.GetAttributeValue("name"),
                        NameInDataStore = element.GetAttributeValue("name"),
                        TypeName = "System.String",
                        TypeNameInDataStore = "System.String",
                        ValueFormat = null,
                        ValueFormatInDataStore = null,
                        ConcurrencyMode = ConcurrencyMode.None,
                    };
                    domainObjectProperty.IsValueType = TypeMapper.IsValueType(domainObjectProperty.PropertyType);
                    domainObjectProperty.IsValueTypeInDataStore = domainObjectProperty.IsValueType;
                    if (!settings.IsExcluded(domainObjectProperty.Name))
                        domainObjectProperty = item.Properties.AddOrGetExisting(domainObjectProperty);
                }
            }

            return domainModel;
        }

        #endregion

        #endregion
        #region Constructors

        private ResourceFileHelper()
        {
        }

        #endregion
    }
}
